I need an array that every item in it have at max one "o" character
array = ["book", "saloon", "dog", "solomon", "cow", "cat", "tire", "window"]
// the array that I need:
newArray = ["dog", "cow", "cat", "tire", "window"]
could anyone help me?
You can use Regular Expressions to count the number of "o" occurrences in the string:
(s.match(/o/gi) || []).length
g Global searchi Assuming you want Case-insensitive searchThen you can Array#filter()
Code:
const array = ["book", "saloon", "dog", "solomon", "cow", "cat", "tire", "window"]
const newArray = array.filter(s => (s.match(/o/gi) || []).length <= 1)
console.log(newArray)